$(document).ready(function() {
	var param = $("#action").html().trim();
	$("#" + param).addClass("focus");

	$(".offline").click(function() {
		if (confirm("你确定要将此商品下架吗？~")) {
			offline("goods", $(this).attr("goodsId"));
		}
	});

	$(".online").click(function() {
		if (confirm("你确定要将此商品重新上架吗？~")) {
			online("goods", $(this).attr("goodsId"));
		}
	});

	$(".offlineTgoods").click(function() {
		if (confirm("你确定要将此团购商品下架吗？此操作不能恢复。")) {
			offline("tgoods", $(this).attr("goodsId"));
		}
	});

});
// 修改商品信息
function changeGoodsInfo() {
	var sellerId = $("#sellerId").html();
	var goodsId = $("#GoodsIdText").html();
	var goodsName = $("#GoodsNameText").val();
	var goodsClass = $("#GoodsClassText").val();
	var goodsPrice = $("#GoodsPriceText").val();
	var goodsDiscount = $("#GoodsDiscountText").val();
	var goodsKeywords = $("#GoodsKeywordsText").val();
	var goodsDesc = $("#GoodsDescText").val().trim();
	if (goodsDiscount > 1.0) {
		alert("折扣输入有误，价格折扣在0-1.0之间。");
		return;
	}
	saveGoodsInfo("change", sellerId, goodsId, goodsName, goodsClass,
			goodsPrice, goodsDiscount, goodsKeywords, goodsDesc, "");
}
// 保存商品信息
function saveGoodsInfo(action, sellerId, goodsId, goodsName, goodsClass,
		goodsPrice, goodsDiscount, goodsKeywords, goodsDesc, goodsUrl) {
	$.ajax({
		type : "POST",
		url : "servlet/MenuManageServlet",
		data : "action=" + action + "&type=goods&sellerId=" + sellerId
				+ "&goodsId=" + goodsId + "&goodsName=" + goodsName
				+ "&goodsClass=" + goodsClass + "&goodsPrice=" + goodsPrice
				+ "&goodsDiscount=" + goodsDiscount + "&goodsKeywords="
				+ goodsKeywords + "&goodsDesc=" + goodsDesc + "&goodsUrl="
				+ goodsUrl,
		success : function(result) {
			var result = result.trim();
			if (result == "success") {
				if (action == "change") {
					alert("菜肴信息已经更新");
					window.location.reload(true);
					return true;
				} else {
					alert("已经成功添加" + goodsName + ",你可以在'销售中的菜肴'选项中查看。");
					window.location.reload(true);
					return true;
				}
			} else {
				$("#ErrorTips").html("信息保存失败");
				return false;
			}
		}
	});
}
// 添加商品
function addGoods() {
	var sellerId = $("#sellerId").html();
	var goodsId = $("#GoodsIdText").html();
	var goodsName = $("#GoodsNameText").val();
	var goodsClass = $("#GoodsClassText").val();
	var goodsPrice = $("#GoodsPriceText").val();
	var goodsDiscount = $("#GoodsDiscountText").val();
	var goodsKeywords = $("#GoodsKeywordsText").val();
	var goodsDesc = $("#GoodsDescText").val().trim();
	var goodsUrl = $(".uploadImg").attr("imagePath");

	if (goodsName == "") {
		alert("商品名不能为空");
		return;
	}
	if (goodsPrice == "") {
		alert("商品价格不能为空");
		return;
	}
	if (goodsDiscount > 1.0) {
		alert("折扣输入有误，价格折扣在0-1.0之间。");
		return;
	}
	saveGoodsInfo("add", sellerId, goodsId, goodsName, goodsClass, goodsPrice,
			goodsDiscount, goodsKeywords, goodsDesc, goodsUrl);
}
// 下架某一商品
function offline(type, goodsId) {
	var sellerId = $("#sellerId").html();
	$.ajax({
		type : "POST",
		url : "servlet/MenuManageServlet",
		data : "action=offline&type=" + type + "&sellerId=" + sellerId
				+ "&goodsId=" + goodsId,
		success : function(result) {
			var result = result.trim();
			if (result == "success") {
				if (type == "goods") {
					// alert("菜肴已下架，你可以在'已下架菜肴'选项中查看");
					window.location.reload(true);
					return true;
				} else {
					alert("该团购商品已成功删除");
					window.location.reload(true);
					return true;
				}
			} else {
				$("#ErrorTips").html("信息保存失败");
				return false;
			}
		}
	});
}
// 上架某一商品
function online(type, goodsId) {
	var sellerId = $("#sellerId").html();
	$.ajax({
		type : "POST",
		url : "servlet/MenuManageServlet",
		data : "action=online&type=" + type + "&sellerId=" + sellerId
				+ "&goodsId=" + goodsId,
		success : function(result) {
			var result = result.trim();
			if (result == "success") {
				if (type == "goods") {
					// alert("菜肴已上架，你可以在'销售中的菜肴'选项中查看");
					window.location.reload(true);
					return true;
				} else {
					alert("该团购商品已成功删除");
					window.location.reload(true);
					return true;
				}
			} else {
				$("#ErrorTips").html("信息保存失败");
				return false;
			}
		}
	});
}

// 添加商品
function addTgoods() {
	var action = "add";
	var sellerId = $("#sellerId").html();
	var goodsName = $("#GoodsNameText").val();
	var goodsClass = $("#GoodsClassText").val();
	var goodsPrice = $("#GoodsPriceText").val();
	var goodsDiscount = $("#GoodsDiscountText").val();
	var goodsKeywords = $("#GoodsKeywordsText").val();
	var goodsDesc = $("#GoodsDescText").val().trim();
	var goodsUrl = $(".uploadImg").attr("imagePath");

	if (goodsName == "") {
		alert("商品名不能为空");
		return;
	}
	if (goodsPrice == "") {
		alert("商品价格不能为空");
		return;
	}
	if (goodsDiscount > 1.0) {
		alert("折扣输入有误，价格折扣在0-1.0之间。");
		return;
	}

	saveTGoodsInfo(action, sellerId, "", goodsName, goodsClass,
			goodsPrice, goodsDiscount, goodsKeywords, goodsDesc, goodsUrl);

}

// 保存商品信息
function saveTGoodsInfo(action, sellerId, goodsId, goodsName, goodsClass,
		goodsPrice, goodsDiscount, goodsKeywords, goodsDesc, goodsUrl) {
	$.ajax({
		type : "POST",
		url : "servlet/MenuManageServlet",
		data : "action=" + action + "&type=tgoods&sellerId=" + sellerId
				+ "&goodsId=" + goodsId + "&goodsName=" + goodsName
				+ "&goodsClass=" + goodsClass + "&goodsPrice=" + goodsPrice
				+ "&goodsDiscount=" + goodsDiscount + "&goodsKeywords="
				+ goodsKeywords + "&goodsDesc=" + goodsDesc + "&goodsUrl="
				+ goodsUrl,
		success : function(result) {
			var result = result.trim();
			if (result == "success") {
				if (action == "change") {
					alert("菜肴信息已经更新");
					window.location.reload(true);
					return true;
				} else {
					alert("已经成功添加团购商品" + goodsName + ",你可以在'团购菜单管理'选项中查看。");
					window.location.reload(true);
					return true;
				}
			} else {
				$("#ErrorTips").html("信息保存失败，请重试");
				return false;
			}
		}
	});
}